import {evaluate, simplify} from "mathjs";
import type {VNode} from "vue";
import type {TableColumnCtx} from "element-plus";
interface SummaryMethodProps<T> {
    columns: TableColumnCtx<T>[]
    data: T[]
}
export function wrapSummaryMethod(propSummaryTypes){
    return (param: SummaryMethodProps) => {
        const { columns, data } = param
        const sums: (string | VNode)[] = []
        columns.forEach((column, index) => {
            if (index === 0) {
                sums[index] = '合计'
                return
            }
            const values = data.map((item) => Number(item[column.property]))
            let m = propSummaryTypes[column.property];
            if (m) {
                sums[index] = m(values)
            } else {
                sums[index] = noop(values)
            }
        })
        return sums
    }
}


export function sum(values:Number[]){
    const expr = values.reduce((prev, curr) => {
        const value = Number(curr)
        if (!Number.isNaN(+value)) {
            return prev  + '+' + curr
        } else {
            return prev
        }
    }, '0')
    console.log(expr)
    return evaluate(simplify(expr).toString())
}

export function avg(values: Number[]){
    let sums = sum(values);
    return evaluate(simplify(sums + '/' + values.length).toString())
}

export function noop(){
    return ''
}